package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.EmptyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.util.calendar.BaseCalendar;

import java.time.LocalDateTime;
import java.util.Date;


/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author tps
 * @since 2021-01-08
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Autowired
    AdChannelMapper adChannelMapper;
    /**
     * @param dto
     * @return 数据列表
     */
    @Override
    public ResponseResult findByNameAndPage(SensitiveDto dto) {
        //1.判断参数是否存在
        if(EmptyUtil.isNullOrEmpty(dto)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<AdSensitive> wrapper = new LambdaQueryWrapper<>();
        if(dto.getSensitive() != null){
            wrapper.like(AdSensitive::getSensitives,dto.getSensitive());
        }
        dto.checkParam();
        Page<AdSensitive> page = new Page<>(dto.getPage(), dto.getSize());


        //2.执行业务
        IPage<AdSensitive> pageResult = page(page, wrapper);


        //3.解析并封装结果返回
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int)pageResult.getTotal());
        result.setData(pageResult.getRecords());
        return result;
    }
    /**
     * @param adSensitive
     * @return 是否插入成功
     */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1判断参数是否可用
        if(EmptyUtil.isNullOrEmpty(adSensitive)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改数据
     * @param adSensitive
     * @return 是否修改
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //1.判断参数是否存在
        if(EmptyUtil.isNullOrEmpty(adSensitive)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive adSensitive1 = getById(adSensitive.getId());
        if(EmptyUtil.isNullOrEmpty(adSensitive1)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive adSensitive = getById(id);
        if(EmptyUtil.isNullOrEmpty(adSensitive)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
